<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use herosphp\utils\ArrayUtils;
use herosphp\filter\Filter;
/***
 * @name :广告控制器 
 * @Class : adAction
 * @Author :yangjifang 
 * @Create : 2016-10-28
 * @copyright : Fiidee.Monda.Cn
 */

class adAction extends CommonAction {

	private $bread = array('信息管理','广告管理');
	private $breadUrl = array('javascript:;','javascript:;');
	protected $serviceBean="information.informationAd.service";
	protected $serviceAdPlaceBean = "information.informationAdPlace.service";

	public function C_start()
	{
		parent::C_start();
	}

	public function add(HttpRequest $request) {
		parent::add($request);
		$this->getAdPlaces();
		$item['ad_place_key'] = $request->getParameter('ad_place_key');
		$this->assign("item", $item);
		$this->assign("admin_name", adminName());
        $this->assign("title", "添加广告");
        $this->assign('bread', array('信息管理','广告列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/ad/index'),'javascript:;'));
	
	}
	public function edit(HttpRequest $request) {
	
		parent::edit($request);
		$this->getAdPlaces();
		$this->assign("admin_name", adminName());
		$this->assign("title", "编辑广告");
        $this->assign('bread', array('信息管理','广告列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/ad/index'),'javascript:;'));
	}
	public function index(HttpRequest $request) {
		$conditions = array();
		$this->assign('_table','广告');
		$s_title = $request->getParameter('s_title','urldecode');
		$this->assign('s_title',$s_title);
		if($s_title !== '' && $s_title!== NULL)$conditions['title'] = '%'.$s_title.'%';

		$s_ad_place_key = $request->getParameter('s_ad_place_key');
		$this->assign('s_ad_place_key',$s_ad_place_key);
		if($s_ad_place_key){
			$conditions['ad_place_key'] = $s_ad_place_key;
		}

		$s_s_addtime = $request->getParameter('s_s_addtime');
		$s_e_addtime = $request->getParameter('s_e_addtime');
		$this->assign('s_s_addtime',$s_s_addtime);
		$this->assign('s_e_addtime',$s_e_addtime);
		if($s_s_addtime !== '' && $s_s_addtime!== NULL){
			$conditions['addtime'] = array();
			$conditions['addtime']['>'] = $s_s_addtime.' 00:00:00';
		}
		if($s_e_addtime !== '' && $s_s_addtime!== NULL)
		{
			if(!$conditions['addtime'])
			{
				$conditions['addtime'] = array();
			}
			$conditions['addtime']['<='] = $s_e_addtime.' 23:59:59';
		}

		$s_s_endtime = $request->getParameter('s_s_endtime');
		$s_e_endtime = $request->getParameter('s_e_endtime');
		$this->assign('s_s_endtime',$s_s_endtime);
		$this->assign('s_e_endtime',$s_e_endtime);
		if($s_s_endtime !== '' && $s_s_endtime!== NULL){
			$conditions['endtime'] = array();
			$conditions['endtime']['>'] = $s_s_endtime.' 00:00:00';
		}
		if($s_e_endtime !== '' && $s_s_endtime!== NULL){
			if(!$conditions['endtime']){ $conditions['endtime'] = array();}
			$conditions['endtime']['<='] = $s_e_endtime.' 23:59:59';
		}


		$s_manager_id = $request->getParameter('s_manager_id');
		$this->assign('s_manager_id',$s_manager_id);
		if($s_manager_id !== '' && $s_manager_id!== NULL)$conditions['manager_id'] = '%'.$s_manager_id.'%';
		$s_app_id = $request->getParameter('s_app_id');
		$this->assign('s_app_id',$s_app_id);
		if($s_app_id !== '' && $s_app_id!== NULL)$conditions['app_id'] = '%'.$s_app_id.'%';
		$this->setConditions($conditions);
	
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);
		$this->setPagesize($pagesize);
		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		//parent::index($request);
		$this->page = $request->getParameter('page', 'intval');
		if ( $this->page <=0 ) {
			$this->page = 1;
		}
        $service = Beans::get($this->getServiceBean());
        $total = $service->count($this->getConditions());
        $items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
            array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
        $this->assign('items', $items);

        if( $request -> getParameter('export') == 1 ){
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this -> setConditions($conditions);
            }
            $this->setPagesize(99999);
            $items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
                array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["information_ad_place_title"];
                $tmp[] = $v["title"];
                $tmp[] = $v["content"];
                $tmp[] = $v["summary"];
                $tmp[] = $v["link"];
                $tmp[] = $v["target"]== 0?'当前窗口':'新窗口';
                $tmp[] = $v["addtime"];
                $tmp[] = $v['remaining_time'];//intval($v["endtime"]) ==0 ?'永久':$v["endtime"];
                $tmp[] = intval($v["endtime"]) ==0 ?'永久':date("Y-m-d",strtotime($v[endtime]));
                $tmp[] = $v["admin_manager_id"];
                $excel[] = $tmp;
            }
            //$this->assign('_filter',array('information_ad_place_id','title','content','summary','link','target','addtime','endtime','admin_manager_id','app_id'));
            $header = array('广告位','标题','内容','描述','广告链接','打开方式','投放开始时间','剩余天数','投放结束时间','操作人');
            phpexcel("广告列表",$header,$excel);
            exit();
        }

        $this->generatePageMenu($total);
		$this->getAdPlaces();
		$this->assign("title", "广告列表");
        $this->assign('bread', array('信息管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
        $this->setView(strtolower($request->getAction()) . '/index');
	}
	public function do_add(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		$checkModel = array(
			"title"=>array(DFILTER_STRING,array(1,50)),
			"content"=>array(DFILETER_STRING,array(2,255)),	
		);		
		Filter::loadFromModel($data,$checkModel,$error);//model可不可以加第四个参数 用函数处理下数据	

		$request->addParameter('data',$data);
		parent::do_add($request);	
	}
	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('data');
		$request->addParameter('data',$data);
		parent::do_edit($request);	
	}
	
	public function getAdPlaces()
	{	
		$adPlaceService = Beans::get($this->serviceAdPlaceBean);
		$adPlaces = $adPlaceService -> getItems(array());
		$adPlaces = ArrayUtils::changeArrayKey($adPlaces);

		$this->assign('adPlaces',$adPlaces);
	}	

}
